package kioscodeal



import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional

@Transactional(readOnly = true)
class ComercioController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
	
	def login = {}
	
	def beforeInterceptor = [action:this.&auth]
	
	def geocodingService

	def auth() {
		if( !(session?.user?.role == Role.ADMINISTRATOR) ){
			flash.message = "Usted debe ser Administrador para realizar esta tarea."
			redirect(uri:'/')
			return false
		}
		else{
			return true
		}
	}

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond Comercio.list(params), model:[comercioInstanceCount: Comercio.count()]
    }
	

    def show(Comercio comercioInstance) {
        respond comercioInstance
    }

    def create() {
        respond new Comercio(params)
    }
	
	@Transactional
	def geolocalize(){
		def comercioInstance = new Comercio(params)
		def coordenada = new Coordenada()
		coordenada.save()
		comercioInstance.coord = coordenada
		def latlng = geocodingService.findLatLngByAddress("${comercioInstance?.calle} ${comercioInstance?.numeroDireccion}, ${comercioInstance?.localidad}")
		if(latlng){
			comercioInstance.coord.lat = latlng.lat
			comercioInstance.coord.lng = latlng.lng
		}
		
		if (!comercioInstance.save(flush: true)) {
			render(view: "create", model: [comercioInstance: comercioInstance])
			return
		}
		flash.message = message(code: 'default.created.message', args: [message(code: 'comercio.label', default: 'Comercio'), comercioInstance.id])
		redirect(action: "show", id: comercioInstance.id)
	}

    @Transactional
    def save(Comercio comercioInstance) {
        if (comercioInstance == null) {
            notFound()
            return
        }

        if (comercioInstance.hasErrors()) {
            respond comercioInstance.errors, view:'create'
            return
        }

        comercioInstance.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'comercio.label', default: 'Comercio'), comercioInstance.id])
                redirect comercioInstance
            }
            '*' { respond comercioInstance, [status: CREATED] }
        }
    }

    def edit(Comercio comercioInstance) {
        respond comercioInstance
    }

    @Transactional
    def update(Comercio comercioInstance) {
        if (comercioInstance == null) {
            notFound()
            return
        }

        if (comercioInstance.hasErrors()) {
            respond comercioInstance.errors, view:'edit'
            return
        }

        comercioInstance.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'Comercio.label', default: 'Comercio'), comercioInstance.id])
                redirect comercioInstance
            }
            '*'{ respond comercioInstance, [status: OK] }
        }
    }

    @Transactional
    def delete(Comercio comercioInstance) {

        if (comercioInstance == null) {
            notFound()
            return
        }

        comercioInstance.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'Comercio.label', default: 'Comercio'), comercioInstance.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'comercio.label', default: 'Comercio'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
